package com.baizhi.service.impl;

import com.baizhi.dao.CmfzBannerDao;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.CmfzBannerService;
import com.baizhi.util.FileUpload;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/8 0008.
 */
@Service
public class CmfzBannerServiceImpl implements CmfzBannerService {
    @Autowired
    private CmfzBannerDao cmfzBannerDao;

    @Override
    public Map queryAll(Integer page, Integer rows) {
        Map map = new HashMap();

        IPage<CmfzBanner> iPage = new Page<>(page, rows);
        QueryWrapper<CmfzBanner> wrapper=new QueryWrapper<>();
        wrapper.ne("banner_state",0);
        IPage<CmfzBanner> cmfzBannerIPage = cmfzBannerDao.selectPage(iPage, wrapper);
        List<CmfzBanner> records = cmfzBannerIPage.getRecords();
        long total = cmfzBannerIPage.getTotal();

        map.put("total", total);
        map.put("rows", records);

        return map;
    }

    @Override
    public Map add(MultipartFile picture, CmfzBanner cmfzBanner, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.upload(picture, cmfzBanner, request);
            cmfzBannerDao.add(cmfzBanner);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }

    @Override
    public Map update(MultipartFile picture, CmfzBanner cmfzBanner, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.updatePicture(picture, cmfzBanner, request);
            cmfzBannerDao.updateById(cmfzBanner);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;

    }

    @Override
    public Map deleteMany(int[] ids) {
        Map map = new HashMap();
        try {
            cmfzBannerDao.deleteMany(ids);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }
}
